//
//  NewsBridge.m
//  suRead
//
//  Created by Ethan on 16/6/23.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "NewsBridge.h"

#import "AccountManager.h"

#import "PBViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ModalViewController.h"


@interface NewsBridge () <PBViewControllerDataSource, PBViewControllerDelegate>
@property (nonatomic, strong) NSArray *frames;
@property (nonatomic, strong) NSMutableArray<UIImageView *> *imageViews;
@property (nonatomic, strong) NSMutableArray *MutableArray;
@end

@implementation NewsBridge


- (void)toH5NewsDetail:(NSArray *)urls {
    if (self.ADCallBack) {
        self.ADCallBack([urls objectAtIndex:0]);
    }
}

- (void)toNewsDetail:(NSArray *)urls {
    if (self.toNewsDetailCallBack) {
        self.toNewsDetailCallBack([urls objectAtIndex:0]);
    }
}

- (void)openImage:(NSArray *)urls {

    NSArray *args = [urls objectAtIndex:0];
    
    self.MutableArray = [@[] mutableCopy];
    self.MutableArray = [NSMutableArray arrayWithArray:args];
    NSString *image = [self.MutableArray objectAtIndex:self.MutableArray.count - 1];

    NSString *atIndex = [image substringFromIndex:9];
    
    for (NSInteger i = self.MutableArray.count - 1;i >= 0;i--)
    {
        NSString *obj = [self.MutableArray objectAtIndex:i];
        if([obj hasPrefix:@"imgindex_"]){
            [self.MutableArray removeObjectAtIndex:i];
        }
    }
    
    self.imageViews = [@[] mutableCopy];
    for (NSInteger index = 0; index < self.MutableArray.count; ++index){
        UIImageView *imageView = [UIImageView new];
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor blackColor];
        imageView.tag =index;
        imageView.userInteractionEnabled = YES;
        NSString *imageName = [NSString stringWithFormat:@"%@", @(index+ 1)];
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]];
        
        [self.imageViews addObject:imageView];
        
    }
    
    // 加载网络图片

    PBViewController *pbViewController = [PBViewController new];
    pbViewController.pb_dataSource = self;
    pbViewController.pb_delegate = self;
    pbViewController.pb_startPage = [atIndex intValue];;
    [[AppDelegate sharedAppDelegate].window.rootViewController presentViewController:pbViewController animated:YES completion:^{
    }];
    
}

- (void)getUserInfo {
    NSString *userid = [AccountManager sharedAccountManager].f_uid;
    NSString *name = [AccountManager sharedAccountManager].f_nickname;
    NSString *mobile= [AccountManager sharedAccountManager].f_phone_num;
    NSString *token = [AccountManager sharedAccountManager].f_authCode;
    NSDictionary *dic = @{@"userid":userid,@"name":name,@"mobile":mobile,@"token":token};
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    [self excuteJSWithObj:nil function:[NSString stringWithFormat:@"passByValue('%@')",jsonString]];
}


#pragma mark - PBViewControllerDataSource

- (NSInteger)numberOfPagesInViewController:(PBViewController *)viewController {
    return self.imageViews.count;
}

- (void)viewController:(PBViewController *)viewController presentImageView:(UIImageView *)imageView forPageAtIndex:(NSInteger)index progressHandler:(void (^)(NSInteger, NSInteger,NSURL *))progressHandler {
    NSString *url = [self.MutableArray objectAtIndex:index];
//    url = [url substringWithRange: NSMakeRange(1, [url length]-2)];
    UIImage *placeholder = self.imageViews[index].image;
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:url]
                 placeholderImage:placeholder
                          options:0
                         progress:progressHandler
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        }];
}

- (UIView *)thumbViewForPageAtIndex:(NSInteger)index {
    return nil;
}

#pragma mark - PBViewControllerDelegate

- (void)viewController:(PBViewController *)viewController didSingleTapedPageAtIndex:(NSInteger)index presentedImage:(UIImage *)presentedImage {
    [[AppDelegate sharedAppDelegate].window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewController:(PBViewController *)viewController didLongPressedPageAtIndex:(NSInteger)index presentedImage:(UIImage *)presentedImage {
    NSLog(@"didLongPressedPageAtIndex: %@", @(index));
}


@end
